package 笔记.多线程;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * @author chenhailong
 * @ClassName: ThreadOne
 * @Description: 线程的三种创建方式
 * @date 2021/8/28 9:22
 * @Version 1.0
 */
public class ThreadOne extends Thread {

    @Override
    public void run(){
        System.out.println("创建线程第一种方式：继承Thread类，重写run()方法");
    }

}

class ThreadTwo implements Runnable{

    @Override
    public void run() {
        System.out.println("创建线程第二种方式：实现Runnable接口，重写run()方法");
    }
}


class ThreadThree implements Callable{

    @Override
    public Object call() throws Exception {
        System.out.println("创建线程第三种方式：实现Callable接口，重写call()方法,并且是有返回值和抛出异常");
        // 执行业务代码
        return 1;
    }
}

class ThreadFour {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("通过匿名内部类创建线程");
            }
        }, "线程名字").start();


        new Thread(() -> {
            System.out.println("通过lambda表达式创建线程，默认是实现runnable接口");
        }).start();

        FutureTask futureTask = new FutureTask(new ThreadThree());
        new Thread(futureTask).start();
        System.out.println(" call多线程返回值:" + futureTask.get());

    }
}
